package com.lzx.user.service.impl;

import com.lzx.common.utils.R;
import com.lzx.user.feign.AnswerClient;
import com.lzx.user.feign.ArticleClient;
import com.lzx.user.feign.RecommendClient;
import com.lzx.user.mapper.UserMapper;
import com.lzx.user.service.EchartsService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author 程序员星星
 * @date 2023/4/18
 * @Description
 */
@Service
public class EchartsServiceImpl implements EchartsService {
    @Resource
    private UserMapper userMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private ArticleClient articleClient;

    @Resource
    private AnswerClient answerClient;

    @Resource
    private RecommendClient recommendClient;

    //统计数据
    @Override
    public R getStatistics() {
        Map<String, Integer> map = new HashMap<>();
        //注册人数
        int registerNum = userMapper.getUserList().size();
        map.put("registerNum", registerNum);
        //在线人数
        int onlineNum = Objects.requireNonNull(stringRedisTemplate.keys("ordinary:*")).size();
        map.put("onlineNum", onlineNum);
        //总文章数
        Map<String, Integer> articleMap = articleClient.getArticleTotal();
        map.put("articleNum", articleMap.get("articleNum"));
        //总问答数
        Map<String, Integer> problemMap = answerClient.getProblemTotal();
        map.put("problemNum", problemMap.get("problemNum"));
        //反馈人数
        map.put("recommendNum", recommendClient.getRecommendTotal());
        //总浏览量
        Integer articleBrowseNum = articleMap.get("browseNum");
        Integer problemBrowseNum = articleMap.get("browseNum");
        map.put("browseNum", articleBrowseNum + problemBrowseNum);
        return R.ok().data("map", map);
    }
}
